package io.finer.erp.jeecg.finance.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.api.client.util.Lists;
import com.google.api.client.util.Maps;

import cn.hutool.core.collection.CollUtil;
import io.finer.erp.jeecg.bas.dto.PrintProductDto;
import io.finer.erp.jeecg.bas.entity.BasSupplier;
import io.finer.erp.jeecg.bas.entity.SalesDelivery;
import io.finer.erp.jeecg.bas.entity.SalesDeliveryEntry;
import io.finer.erp.jeecg.bas.service.IBasSupplierService;
import io.finer.erp.jeecg.bas.service.ISalesDeliveryEntryService;
import io.finer.erp.jeecg.bas.service.ISalesDeliveryService;
import io.finer.erp.jeecg.bas.service.IZrkDeliverEntryService;
import io.finer.erp.jeecg.bas.vo.PrintProductVO;
import io.finer.erp.jeecg.finance.dto.FinaFurlDTO;
import io.finer.erp.jeecg.finance.dto.FinaFurlEntryDTO;
import io.finer.erp.jeecg.finance.dto.FinaFurlSumDTO;
import io.finer.erp.jeecg.finance.dto.FinaPaymentDto;
import io.finer.erp.jeecg.finance.dto.FinaReceiptDto;
import io.finer.erp.jeecg.finance.dto.ReceiveDTO;
import io.finer.erp.jeecg.finance.entity.FinaPayment;
import io.finer.erp.jeecg.finance.service.IFinaPaymentService;
import io.finer.erp.jeecg.finance.vo.CGDIoBillFKVo;
import io.finer.erp.jeecg.finance.vo.DeliverDetalidVO;
import io.finer.erp.jeecg.finance.vo.DeliverySummaryVO;
import io.finer.erp.jeecg.finance.vo.FinaFurlVO;
import io.finer.erp.jeecg.finance.vo.FinaPaymentAddVo;
import io.finer.erp.jeecg.finance.vo.FinaPaymentPage;
import io.finer.erp.jeecg.finance.vo.FinaPaymentPageSum;
import io.finer.erp.jeecg.finance.vo.FinaPaymentSumPage;
import io.finer.erp.jeecg.finance.vo.FinaReceiptPage;
import io.finer.erp.jeecg.finance.vo.FinaReceiptSumVO;
import io.finer.erp.jeecg.finance.vo.FinaReceiptVO;
import io.finer.erp.jeecg.finance.vo.PaymentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 付款表
 * @Author: jeecg-boot
 * @Date: 2021-03-22
 * @Version: V1.0
 */
@Api(tags = "付款表")
@RestController
@RequestMapping("/finance/finaPayment")
@Slf4j
public class FinaPaymentController extends JeecgController<FinaPayment, IFinaPaymentService> {
	@Autowired
	private IFinaPaymentService finaPaymentService;
	@Autowired
	private IBasSupplierService supplierService;
	@Autowired
	private IZrkDeliverEntryService zrkDeliverEntryService;
	@Autowired
	private ISalesDeliveryService deliveryService;
	@Autowired
	private ISalesDeliveryEntryService deliveryEntryService;
	@Autowired
	private ISysBaseAPI iSysBaseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param finaPayment
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "付款表-分页列表查询")
	@ApiOperation(value = "付款表-分页列表查询", notes = "付款表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FinaPayment finaPayment,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		QueryWrapper<FinaPayment> queryWrapper = QueryGenerator.initQueryWrapper(finaPayment, req.getParameterMap());
		Page<FinaPayment> page = new Page<FinaPayment>(pageNo, pageSize);
		IPage<FinaPayment> pageList = finaPaymentService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	@AutoLog(value = "付款表-分页列表查询-新方法")
	@ApiOperation(value = "付款表-分页列表查询-新方法", notes = "付款表-分页列表查询-新方法")
	@GetMapping(value = "/paymentList")
	public Result<?> paymentList(FinaPayment finaPayment,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
//		QueryWrapper<FinaPayment> queryWrapper = QueryGenerator.initQueryWrapper(finaPayment, req.getParameterMap());
		Page<FinaPayment> page = new Page<FinaPayment>(pageNo, pageSize);
		IPage<FinaPayment> pageList = finaPaymentService.paymentList(page, req);
		List<DictModel> dictItemList = iSysBaseAPI.queryDictItemsByCode("clearingForm");
		if (CollUtil.isNotEmpty(dictItemList)) {
			Map<String, DictModel> dictItemMap = Maps.newHashMap();
			for (DictModel dictModel : dictItemList) {
				dictItemMap.put(dictModel.getValue(), dictModel);
			}
			List<FinaPayment> records = pageList.getRecords();
			for (FinaPayment payment : records) {
				String clearingForm = payment.getClearingForm();
				if (StringUtils.isNotBlank(clearingForm)) {
					DictModel dictModel = dictItemMap.get(clearingForm);
					if (null != dictModel) {
						payment.setClearingForm_dictText(dictModel.getText());
					}
				}
			}
		}
//		IPage<FinaPayment> pageList = finaPaymentService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaPayment
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "付款表-付款统计查询")
	@ApiOperation(value = "付款表-付款统计查询", notes = "付款表-付款统计查询")
	@GetMapping(value = "/sumList")
	public Result<?> sumList(FinaPaymentPage finaPayment,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Page<FinaPaymentPage> page = new Page<FinaPaymentPage>(pageNo, pageSize);
		List<FinaPaymentPage> list = finaPaymentService.getPayment(finaPayment);
		return Result.ok(page.setRecords(list));
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * 
	 * @return
	 */
	@AutoLog(value = "列表金额合计")
	@ApiOperation(value = "列表金额合计", notes = "列表金额合计")
	@GetMapping(value = "/listZDSum")
	public Result<?> queryPageListSum(FinaPaymentPage finaPayment) {

		BigDecimal receivablesEnd = finaPayment.getReceivablesEnd();
		BigDecimal receivablesStart = finaPayment.getReceivablesStart();
		BigDecimal receivedsEnd = finaPayment.getReceivedsEnd();
		BigDecimal receivedsStart = finaPayment.getReceivedsStart();
		BigDecimal invoicePriceStart = finaPayment.getInvoicePriceStart();
		BigDecimal invoicePriceEnd = finaPayment.getInvoicePriceEnd();
		BigDecimal discountSumPriceStart = finaPayment.getDiscountSumPriceStart();
		BigDecimal discountSumPriceEnd = finaPayment.getDiscountSumPriceEnd();

		FinaPaymentPageSum pageList = finaPaymentService.getPageReceiptSum(finaPayment.getCreateTimeStart(),
				finaPayment.getCreateTimeEnd(), finaPayment.getSupplierName(), receivablesEnd, receivablesStart,
				receivedsEnd, receivedsStart, invoicePriceStart, invoicePriceEnd, discountSumPriceStart,
				discountSumPriceEnd);
		return Result.ok(pageList);

	}

	/**
	 * 分页列表查询
	 *
	 * @param finaPayment
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "付款表-当天已付款分页列表查询")
	@ApiOperation(value = "付款表-当天已付款分页列表查询", notes = "付款表-当天已付账单分页列表查询")
	@GetMapping(value = "/listFK")
	public Result<?> queryPageFKList(FinaPayment finaPayment,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		QueryWrapper<FinaPayment> queryWrapper = QueryGenerator.initQueryWrapper(finaPayment, req.getParameterMap());
		Page<FinaPayment> page = new Page<FinaPayment>(pageNo, pageSize);
//		"createTimeStart":["2021-06-15"],"createTimeEnd":["2021-06-16"],"supplierName":["11111"]
		/*************************** 默认当月 *****************************************/
		// 获取本月第一天
		/*
		 * Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date());
		 * calendar.set(Calendar.DAY_OF_MONTH,
		 * calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); Date s =
		 * calendar.getTime(); //获取本月最后一天 Calendar calendar2 = Calendar.getInstance();
		 * calendar2.setTime(new Date()); calendar2.set(Calendar.DAY_OF_MONTH,
		 * calendar2.getActualMaximum(Calendar.DAY_OF_MONTH)); Date e =
		 * calendar2.getTime(); queryWrapper.between("create_time",
		 * DateUtils.formatDate(s, "yyyy/MM/dd"), DateUtils.formatDate(e,
		 * "yyyy/MM/dd"));
		 */
		/*****************************
		 * 默认当天
		 **********************************************/
		queryWrapper.eq("create_time", DateUtils.formatDate(new Date(), "yyyy/MM/dd"));
		/*********************************************************************************/
		queryWrapper.eq("bill_type", "4");
		queryWrapper.groupBy("supplier_Id");
		IPage<FinaPayment> pageList = finaPaymentService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaReceipt
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "根据厂商id查询订单信息")
	@ApiOperation(value = "根据厂商id查询订单信息", notes = "根据厂商id查询订单信息")
	@GetMapping(value = "/furlList")
	public Result<?> queryPageDeleiverList(FinaFurlDTO entryVO,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Map<String, Object> map = finaPaymentService.getFinaFurl(entryVO.getType(), entryVO.getSupplierId(),
				entryVO.getCreateTimeStart(), entryVO.getCreateTimeEnd(), entryVO.getSalesType(), entryVO.getCode(),
				entryVO.getProductName(), entryVO.getViceQtyStart(), entryVO.getViceQtyEnd(), entryVO.getQtyStart(),
				entryVO.getQtyEnd(), entryVO.getUnitPriceStart(), entryVO.getUnitPriceEnd(),
				entryVO.getTotalPriceStart(), entryVO.getTotalPriceEnd(), entryVO.getTotalAmountStart(),
				entryVO.getTotalAmountEnd(), entryVO.getHxPriceStart(), entryVO.getHxPriceEnd(),
				entryVO.getZkPriceStart(), entryVO.getZkPriceEnd(), entryVO.getQkPriceStart(), entryVO.getQkPriceEnd(),
				entryVO.getRemark());
		return Result.ok(map);
	}

	/**
	 * 添加
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "付款表-添加平账信息")
	@ApiOperation(value = "付款表-添加平账信息", notes = "付款表-添加平账信息")
	@PostMapping(value = "/add")
	public Result<?> addMarch(@RequestBody FinaPaymentAddVo paymentAddVo) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		FinaPayment payment = new FinaPayment();
		BasSupplier supplier = supplierService.getById(paymentAddVo.getSupplierId());
		String suppName = "";
		if (supplier != null) {
			suppName = supplier.getName();
			payment.setSupplierName(suppName);
		}
		if (null != paymentAddVo && paymentAddVo.getDiscountPrice() == null) {
			paymentAddVo.setDiscountPrice(BigDecimal.ZERO);
		}
		String code = this.iSysBaseAPI.getNoByCode("cw_fk_no");
		payment.setPrice(paymentAddVo.getPrice());
		payment.setDiscountPrice(paymentAddVo.getDiscountPrice());
		payment.setAccountId(paymentAddVo.getAccountId());
		payment.setSupplierId(paymentAddVo.getSupplierId());
		payment.setCode(code);
		payment.setBillType("4");
		payment.setClearingForm(paymentAddVo.getClearingForm());
		finaPaymentService.save(payment);
//		 finaPaymentService.marchFlat(payment);
		// 核销订单
		finaPaymentService.automaticWriteOff(paymentAddVo);
		SalesDelivery delivery = new SalesDelivery();// 发货单收款信息
		delivery.setCode(code);
		delivery.setHxPrice(paymentAddVo.getPrice());
		delivery.setZkPrice(paymentAddVo.getDiscountPrice());
		delivery.setCustomerId(paymentAddVo.getSupplierId());
		delivery.setCustomerName(suppName);
		delivery.setShippingDate(new Date());
		delivery.setSalesType(1);
		delivery.setMaker(sysUser.getRealname());

		delivery.setHxId(payment.getId());
		deliveryService.save(delivery);// 添加发货单收款信息
		SalesDeliveryEntry entry = new SalesDeliveryEntry();
		String dictName = iSysBaseAPI.queryDictTextByKey("clearingForm", payment.getClearingForm());
		entry.setProductName(dictName);
		entry.setCustomerId(delivery.getCustomerId());
		entry.setDeliveryId(delivery.getId());
		deliveryEntryService.save(entry);// 保存发货单收款明细---其实基本就一个结算方式~~
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param finaPayment
	 * @return
	 */
	@AutoLog(value = "付款表-编辑")
	@ApiOperation(value = "付款表-编辑", notes = "付款表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FinaPayment finaPayment) {
		finaPaymentService.updateById(finaPayment);
		finaPaymentService.marchFlat(finaPayment);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "付款表-通过id删除")
	@ApiOperation(value = "付款表-通过id删除", notes = "付款表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		finaPaymentService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "付款表-批量删除")
	@ApiOperation(value = "付款表-批量删除", notes = "付款表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.finaPaymentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "付款表-通过id查询")
	@ApiOperation(value = "付款表-通过id查询", notes = "付款表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		FinaPayment finaPayment = finaPaymentService.getById(id);
		if (finaPayment == null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(finaPayment);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param finaPayment
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, FinaPayment finaPayment) {
		return export(request, finaPayment, "付款表");
	}

	protected ModelAndView export(HttpServletRequest request, FinaPayment object, String title) {
		// 获取本月第一天
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		Date s = calendar.getTime();
		// 获取本月最后一天
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(new Date());
		calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date e = calendar2.getTime();

		String supplierId = object.getSupplierId();
		object.setSupplierId("");
		// Step.1 组装查询条件
		QueryWrapper<FinaPayment> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
		queryWrapper.between("create_time", DateUtils.formatDate(s, "yyyy/MM/dd"),
				DateUtils.formatDate(e, "yyyy/MM/dd"));
		queryWrapper.eq("supplier_id", supplierId);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<FinaPayment> pageList = finaPaymentService.list(queryWrapper);
		List<PrintProductDto> listss = Lists.newArrayList();
		List<PrintProductDto> list = Lists.newArrayList();
		for (FinaPayment receipt : pageList) {
			list = zrkDeliverEntryService.exportIoBill(receipt.getBillNo());
			listss.addAll(list);
			list.clear();
		}

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, PrintProductDto.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, listss);
		return mv;
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param finaPayment
	 */
	@RequestMapping(value = "/exportSum")
	public ModelAndView exportSum(HttpServletRequest request, FinaPaymentDto finaPayment) {
		return exportSum(request, finaPayment, "付款表");
	}

	// 导出当月统计账单数据
	protected ModelAndView exportSum(HttpServletRequest request, FinaPaymentDto object, String title) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
//		 object.getCreateTimeStart(),
//				 object.getCreateTimeEnd(),object.getCustomerName()
		List<FinaPaymentDto> pageList = finaPaymentService.getPaymentEx(object.getCreateTimeStart(),
				object.getCreateTimeEnd(), object.getSupplierName());
		List<List<PrintProductVO>> lists = Lists.newArrayList();

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, FinaReceiptDto.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, FinaPayment.class);
	}

	/*************************************************************************************************/

	/*
	 *
	 * @author libaow
	 * 
	 * @date 2021/4/16 14:20
	 * 
	 * @param finaPayment
	 * 
	 * @return org.jeecg.common.api.vo.Result<?>
	 */
	@AutoLog(value = "付款单-统计金额总数")
	@ApiOperation(value = "付款单-统计金额总数", notes = "付款单-统计金额总数")
	@GetMapping(value = "/amountFk")
	public Result<?> amount(FinaPayment finaPayment) {
		FinaReceiptSumVO getamount = finaPaymentService.getamountFK();
		return Result.ok(getamount);
	}

	/**
	 * 分页列表查询
	 *
	 * @param finaPayment
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "付款表-付款汇总统计")
	@ApiOperation(value = "付款表-付款汇总统计查询", notes = "付款表-付款汇总统计查询")
	@GetMapping(value = "/listSum")
	public Result<?> sumlist(FinaPaymentSumPage finaPayment,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
//		 QueryWrapper<FinaPayment> queryWrapper = QueryGenerator.initQueryWrapper(finaPayment, req.getParameterMap());
		Page<FinaPaymentSumPage> page = new Page<FinaPaymentSumPage>(pageNo, pageSize);
		IPage<FinaPaymentSumPage> pageList = finaPaymentService.getSumPayment(page, finaPayment.getCreateTimeStart(),
				finaPayment.getCreateTimeEnd(), finaPayment.getSupplierName());
		return Result.ok(pageList);
	}

	@AutoLog(value = "付款单-获取采购单列表")
	@ApiOperation(value = "付款单-获取采购单列表", notes = "付款单-获取采购单列表")
	@GetMapping(value = "/orderFK")
	public Result<?> getOrder(CGDIoBillFKVo cgdIoBillFKVo) {
		List<CGDIoBillFKVo> dtos = finaPaymentService.getCGDIoBill(cgdIoBillFKVo);
		return Result.ok(dtos);
	}

	@AutoLog(value = "付款单-获取供应商的进货信息")
	@ApiOperation(value = "付款单-获取供应商的进货信息", notes = "付款单-获取供应商的进货信息")
	@ApiImplicitParam(name = "supplierId", value = "供应商ID", required = true)
	@GetMapping(value = "/getSupplierReceive")
	public Result<List<ReceiveDTO>> getSupplierReceive(@RequestParam("supplierId") String supplierId) {
		Result<List<ReceiveDTO>> result = new Result<>();
		List<ReceiveDTO> list = this.finaPaymentService.getSupplierReceive(supplierId);
		result.setResult(list);
		return result;
	}

	@PostMapping("/addPayment")
	@ApiOperation(value = "新增付款单", notes = "新增付款单")
	@AutoLog(value = "新增付款单")
	public Result<?> addPayment(@RequestBody PaymentVO paymentVO) {
		return this.finaPaymentService.addPayment(paymentVO);
	}

	@PostMapping("/updatePayment")
	@ApiOperation(value = "修改付款单", notes = "修改付款单")
	@AutoLog(value = "修改付款单")
	public Result<?> updatePayment(@RequestBody PaymentVO paymentVO) {
		return this.finaPaymentService.updatePayment(paymentVO);
	}

	@GetMapping("/getPaymentInfo")
	@ApiOperation(value = "付款单详情", notes = "付款单详情")
	@AutoLog(value = "付款单详情")
	public Result<PaymentVO> getPaymentInfo(String paymentId) {
		return this.finaPaymentService.getPaymentInfo(paymentId);
	}

	/***************************************
	 * 收货
	 *******************************************************/

	@AutoLog(value = "付款单-收货汇总")
	@ApiOperation(value = "付款单-收货汇总", notes = "付款单-收货汇总")
	@GetMapping(value = "/getFurl")
	public Result<?> getFurl(FinaFurlVO finaFurlVO) {
		List<FinaFurlSumDTO> dtos = finaPaymentService.getFurlSum(finaFurlVO);
		if (dtos == null || dtos.size() == 0) {
			dtos = Lists.newArrayList();
		}
		return Result.ok(dtos);
	}

	@AutoLog(value = "付款单-收货明细汇总")
	@ApiOperation(value = "付款单-收货明细汇总", notes = "付款单-收货明细汇总")
	@GetMapping(value = "/getFurlEntry")
	public Result<?> getFurlEntry(FinaFurlVO finaFurlVO) {
		List<FinaFurlEntryDTO> dtos = finaPaymentService.getFurlEntrySum(finaFurlVO);
		if (dtos == null || dtos.size() == 0) {
			dtos = Lists.newArrayList();
		}
		return Result.ok(dtos);
	}

	/**
	 * 收货汇总导出excel
	 *
	 * @param request
	 * @param finaReceipt
	 */
	@RequestMapping(value = "/exportXlsDeliver")
	public ModelAndView exportXlsDeliver(FinaFurlVO detalidVO) {
		return exportDeliver(detalidVO, "收货汇总");
	}

	// 导出当月统计账单数据
	protected ModelAndView exportDeliver(FinaFurlVO object, String title) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<FinaFurlSumDTO> pageList = finaPaymentService.getFurlSum(object);

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, DeliverySummaryVO.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * 收货明细汇总导出excel
	 *
	 * @param request
	 * @param finaReceipt
	 */
	@RequestMapping(value = "/exportXlsEntry")
	public ModelAndView exportXlsEntry(FinaFurlVO detalidVO) {
		return exportEntry(detalidVO, "收货明细汇总");
	}

	// 导出当月统计账单数据
	protected ModelAndView exportEntry(FinaFurlVO object, String title) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<FinaFurlEntryDTO> pageList = finaPaymentService.getFurlEntrySum(object);

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, DeliverDetalidVO.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * @Description 付款单统计
	 * @param finaPayment
	 * @param req
	 * @return Create at: 2021-06-21 17:33:26
	 * @author: duany Revision: 2021-06-21 17:33:26 - first revision by duany
	 * 
	 */
	@GetMapping("/getPaymentStatistic")
	public Result<?> getPaymentStatistic(FinaPayment finaPayment, HttpServletRequest req) {
		return this.finaPaymentService.getPaymentStatistic(finaPayment, req);
	}
	
	/**
	 * @Description 应付账款余额表导出
	 * @param finaPaymentPage
	 * @param req
	 * @return
	 * Create at: 2021-09-19 15:33:27
	 * @author: xzs
	 * Revision:
	 *    2021-09-19 15:33:27 - first revision by xzs
	 *
	 */
	@RequestMapping(value = "/exportExcelPayable")
	public ModelAndView exportExcelPayable(FinaPaymentPage finaPaymentPage, HttpServletRequest req) {
		String title = "应付账款余额";
		List<FinaPaymentPage> list = this.finaPaymentService.getPayment(finaPaymentPage);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title); // 此处设置的filename无效 ,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.CLASS, FinaPaymentPage.class);
		mv.addObject(NormalExcelConstants.PARAMS,
				new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}
}
